package com.c201801020208.ybl.User;

import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.c201801020208.ybl.Adapters.MainFragmentPageAdapter;
import com.c201801020208.ybl.R;
import com.c201801020208.ybl.Adapters.FragmentAdapter;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class MainFragment extends Fragment {
    public static String[] mTitles = new String[]{
            "医师医技", "健康整形", "医疗销售", "医疗器械", "生物制药", "护理", "医药编辑"};
    private List<MyFragment> fragments = new ArrayList<MyFragment>();
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private FragmentAdapter adapter;
    private int curTab = 0;

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);

        return view;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 创建fragment对象
        tabLayout = (TabLayout) getActivity().findViewById(R.id.tabLayout);
        viewPager = (ViewPager) getActivity().findViewById(R.id.viewpager);
        initData();

        //给ViewPager设置适配器
        adapter = new FragmentAdapter(getChildFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabsFromPagerAdapter(adapter);
        tabLayout.setSmoothScrollingEnabled(true);
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        tabLayout.setTabTextColors(getResources().getColor(R.color.colorGray1, null), getResources().getColor(R.color.colorWhite, null));
        tabLayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.colorWhite, null));
        tabLayout.setTabIndicatorFullWidth(true);


        changeTabsFont();
        viewPager.setCurrentItem(curTab);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                ((MyFragment) adapter.getItem(position)).sendMessage();
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

    }

    private void initData() {
        for (int i = 0; i < mTitles.length; i++) {
            MyFragment fragment = new MyFragment(curTab, mTitles[i].toString());
            fragment.setTabPos(i);
            fragments.add(fragment);
        }
    }

    //设置字体
    private void changeTabsFont() {
        ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0);
        int tabsCount = vg.getChildCount();
        for (int j = 0; j < tabsCount; j++) {
            ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);
            int tabChildsCount = vgTab.getChildCount();
            for (int i = 0; i < tabChildsCount; i++) {
                View tabViewChild = vgTab.getChildAt(i);
                if (tabViewChild instanceof TextView) {
                    Typeface mTypeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/STKAITI.TTF");
                    ((TextView) tabViewChild).setTypeface(mTypeface);
                }
            }
        }
    }
}
